package com.lxw.manage_media_process.mq;

import com.alibaba.fastjson.JSON;
import com.lxw.framework.domain.media.MediaFile;
import com.lxw.framework.domain.media.MediaFileProcess_m3u8;
import com.lxw.framework.utils.HlsVideoUtil;
import com.lxw.framework.utils.Mp4VideoUtil;
import com.lxw.manage_media_process.dao.MediaFileRepository;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Component
public class MediaProcessTask {

    @Value("${yh-service-manage-media.video-location}")
    private String videoPath;
    @Value("${yh-service-manage-media.ffmpeg-path}")
    private String ffmpegPath;
    @Autowired
    private MediaFileRepository mediaFileRepository;

    @RabbitListener(queues = "${yh-service-manage-media.mq.queue-media-video-processor}")
    public void receiveMessage(String msg){
        Map map = JSON.parseObject(msg, Map.class);
        String mediaId = map.get("mediaId").toString();
        process(mediaId);
    }

    private void process(String mediaId){
        Optional<MediaFile> optional = mediaFileRepository.findById(mediaId);
        MediaFile mediaFile = optional.get();
        if (mediaFile.getFileType()==null || !mediaFile.getFileType().equals("trec")){
            mediaFile.setProcessStatus("303004");
            mediaFileRepository.save(mediaFile);
            return;
        }else{
            mediaFile.setProcessStatus("303001");
            mediaFileRepository.save(mediaFile);
        }
        //生成mp4
        String mp4Path = videoPath+ mediaFile.getFilePath();
        Mp4VideoUtil mp4VideoUtil = new Mp4VideoUtil(ffmpegPath,videoPath+mediaFile.getFilePath()+mediaFile.getFileName(),
                mediaFile.getFileId()+".mp4", mp4Path);
        String flag = mp4VideoUtil.generateMp4();
        if (flag ==null || !flag.equals("success")){
            mediaFile.setProcessStatus("303003");
            MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
            mediaFileProcess_m3u8.setErrormsg(flag);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
            mediaFileRepository.save(mediaFile);
            return;
        }
        //转换m3u8文件
        String m3u8Path = videoPath+ mediaFile.getFilePath()+"hls/";
        String m3u8FileName = mediaFile.getFileId()+".m3u8";
        HlsVideoUtil hlsVideoUtil = new HlsVideoUtil(ffmpegPath,mp4Path+mediaFile.getFileId()+".mp4",m3u8FileName,m3u8Path);
        String generateM3u8 = hlsVideoUtil.generateM3u8();
        if (generateM3u8 ==null || !generateM3u8.equals("success")){
            mediaFile.setProcessStatus("303003");
            MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
            mediaFileProcess_m3u8.setErrormsg(flag);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
            mediaFileRepository.save(mediaFile);
            return;
        }else {
            List<String> ts_list = hlsVideoUtil.get_ts_list();
            MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
            mediaFileProcess_m3u8.setTslist(ts_list);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
            mediaFile.setProcessStatus("303002");
            mediaFile.setFileUrl(mediaFile.getFilePath()+"hls/"+m3u8FileName);
            mediaFileRepository.save(mediaFile);
        }

    }
}
